﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Calibration.CUiCalibrationAddPairDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Calibration.Resources;
using System;
using System.CodeDom.Compiler;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.Calibration;

public class CUiCalibrationAddPairDlg : TxWindow, IComponentConnector
{
  private CApCalibrationSetsModel _CalibrationSetsModel;
  internal TxObjEditBoxControl CalibrationPairVirtualObject;
  internal TxObjEditBoxControl CalibrationPairActualObject;
  internal System.Windows.Controls.Button AddButton;
  internal System.Windows.Controls.Button CloseButton;
  private bool _contentLoaded;

  public CUiCalibrationAddPairDlg(CApCalibrationSetsModel csm)
  {
    // ISSUE: unable to decompile the method.
  }

  private void CUiCalibrationAddPairDlg_Closed(object sender, EventArgs e)
  {
    this.CalibrationPairVirtualObject.LoseFocus();
    this.CalibrationPairActualObject.LoseFocus();
  }

  private void CalibrationPairVirtualObject_Picked(
    object sender,
    TxObjEditBoxCtrl_PickedEventArgs args)
  {
    if (!this.CalibrationPairVirtualObject.IsValid())
      return;
    this.CalibrationPairActualObject.Focus();
    this.CalibrationPairActualObject.Select();
    this.AddButton.IsEnabled = this.CalibrationPairVirtualObject.IsValid() && this.CalibrationPairActualObject.IsValid();
  }

  private void CalibrationPairActualObject_Picked(
    object sender,
    TxObjEditBoxCtrl_PickedEventArgs args)
  {
    if (this.CalibrationPairVirtualObject.IsValid() && this.CalibrationPairActualObject.IsValid())
    {
      this.CalibrationPairActualObject.LoseFocus();
      this.AddButton.IsEnabled = true;
      this.AddButton.Focus();
    }
    else
      this.AddButton.IsEnabled = false;
  }

  private void AddButton_Click(object sender, RoutedEventArgs e)
  {
    string calibrationPairTitle = CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_TITLE;
    if (this.CalibrationPairVirtualObject.IsValid() && this.CalibrationPairActualObject.IsValid() && !this.CalibrationPairVirtualObject.Object.Equals((object) this.CalibrationPairActualObject.Object))
    {
      if (this.CalibrationPairVirtualObject.Object is ITxOrderedCompoundOperation)
      {
        if (this.CalibrationPairActualObject.Object is ITxOrderedCompoundOperation)
        {
          ITxOrderedCompoundOperation compoundOperation1 = this.CalibrationPairVirtualObject.Object as ITxOrderedCompoundOperation;
          ITxOrderedCompoundOperation compoundOperation2 = this.CalibrationPairActualObject.Object as ITxOrderedCompoundOperation;
          TxTypeFilter txTypeFilter = new TxTypeFilter();
          txTypeFilter.AddIncludedType(typeof (ITxLocationOperation));
          TxObjectList directDescendants1 = ((ITxObjectCollection) compoundOperation1).GetDirectDescendants((ITxTypeFilter) txTypeFilter);
          TxObjectList directDescendants2 = ((ITxObjectCollection) compoundOperation2).GetDirectDescendants((ITxTypeFilter) txTypeFilter);
          if (((Collection<ITxObject>) directDescendants1).Count == 0)
          {
            int num = (int) TxMessageBox.Show(string.Format(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_ZERO_SIZE, (object) ((ITxObject) compoundOperation1).Name), calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            return;
          }
          if (((Collection<ITxObject>) directDescendants2).Count == 0)
          {
            int num = (int) TxMessageBox.Show(string.Format(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_ZERO_SIZE, (object) ((ITxObject) compoundOperation2).Name), calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            return;
          }
          if (((Collection<ITxObject>) directDescendants1).Count == ((Collection<ITxObject>) directDescendants2).Count)
          {
            string str = "";
            bool flag = false;
            for (int index = 0; index < ((ITxObjectCollection) compoundOperation1).Count; ++index)
            {
              CApCalibrationPair cp = new CApCalibrationPair();
              cp.VirtualPoint = (ITxLocatableObject) ((Collection<ITxObject>) directDescendants1)[index];
              cp.ActualPoint = (ITxLocatableObject) ((Collection<ITxObject>) directDescendants2)[index];
              if (!this._CalibrationSetsModel.SelectedCalibrationSet.CalibrationTransform.CheckIfLocationExists(cp.VirtualPoint))
              {
                if (!this._CalibrationSetsModel.SelectedCalibrationSet.CalibrationTransform.CheckIfLocationExists(cp.ActualPoint))
                {
                  this._CalibrationSetsModel.AddCalibrationPairToSet(this._CalibrationSetsModel.SelectedCalibrationSet, cp);
                }
                else
                {
                  if (!flag)
                  {
                    str = $"{str}{CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_PATH_ALREADY_EXISTS}\n\n";
                    flag = true;
                  }
                  str = $"{str}- {string.Format(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_PATH_ALREADY_EXISTS_PAIR, (object) ((ITxObject) cp.ActualPoint).Name, (object) ((ITxObject) cp.VirtualPoint).Name, (object) ((ITxObject) cp.ActualPoint).Name)}\n";
                }
              }
              else
              {
                if (!flag)
                {
                  str = $"{str}{CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_PATH_ALREADY_EXISTS}\n\n";
                  flag = true;
                }
                str = $"{str}- {string.Format(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_PATH_ALREADY_EXISTS_PAIR, (object) ((ITxObject) cp.VirtualPoint).Name, (object) ((ITxObject) cp.VirtualPoint).Name, (object) ((ITxObject) cp.ActualPoint).Name)}\n";
              }
            }
            this._CalibrationSetsModel.SelectedCalibrationSet = this._CalibrationSetsModel.SelectedCalibrationSet;
            if (flag)
            {
              int num = (int) TxMessageBox.Show(str, calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
          }
          else
          {
            int num = (int) TxMessageBox.Show(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_WRONG_SIZE, calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            return;
          }
        }
        else
        {
          int num = (int) TxMessageBox.Show(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_WRONG_TYPE, calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
          return;
        }
      }
      else
      {
        if (this.CalibrationPairActualObject.Object is ITxOperation && !(this.CalibrationPairActualObject.Object is ITxLocationOperation))
        {
          int num = (int) TxMessageBox.Show(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_WRONG_TYPE, calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
          return;
        }
        CApCalibrationPair cp = new CApCalibrationPair();
        cp.VirtualPoint = (ITxLocatableObject) this.CalibrationPairVirtualObject.Object;
        cp.ActualPoint = (ITxLocatableObject) this.CalibrationPairActualObject.Object;
        if (!this._CalibrationSetsModel.SelectedCalibrationSet.CalibrationTransform.CheckIfLocationExists(cp.VirtualPoint))
        {
          if (!this._CalibrationSetsModel.SelectedCalibrationSet.CalibrationTransform.CheckIfLocationExists(cp.ActualPoint))
          {
            this._CalibrationSetsModel.AddCalibrationPairToSet(this._CalibrationSetsModel.SelectedCalibrationSet, cp);
          }
          else
          {
            int num = (int) TxMessageBox.Show(string.Format(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_ALREADY_EXISTS, (object) ((ITxObject) cp.ActualPoint).Name), calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            this.CalibrationPairActualObject.Focus();
            this.CalibrationPairActualObject.Select();
            return;
          }
        }
        else
        {
          int num = (int) TxMessageBox.Show(string.Format(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_ALREADY_EXISTS, (object) ((ITxObject) cp.VirtualPoint).Name), calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
          this.CalibrationPairVirtualObject.Focus();
          this.CalibrationPairVirtualObject.Select();
          return;
        }
      }
      this.CalibrationPairVirtualObject.Text = "";
      this.CalibrationPairVirtualObject.Object = (ITxObject) null;
      this.CalibrationPairActualObject.Text = "";
      this.CalibrationPairActualObject.Object = (ITxObject) null;
      this.CalibrationPairVirtualObject.Focus();
      this.CalibrationPairVirtualObject.Select();
      this.AddButton.IsEnabled = false;
    }
    else
    {
      int num1 = (int) TxMessageBox.Show(string.Format(CalibrationResources.CALIBRATION_ADD_CALIBRATION_PAIR_IDENTICAL_ENTRIES), calibrationPairTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  private void CloseButton_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void CalibrationPairVirtualObject_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  {
    this.AddButton.IsEnabled = this.CalibrationPairVirtualObject.IsValid() && this.CalibrationPairActualObject.IsValid();
  }

  private void CalibrationPairActualObject_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  {
    this.AddButton.IsEnabled = this.CalibrationPairVirtualObject.IsValid() && this.CalibrationPairActualObject.IsValid();
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/calibration/cuicalibrationaddpairdlg.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.CalibrationPairVirtualObject = (TxObjEditBoxControl) target;
        break;
      case 2:
        this.CalibrationPairActualObject = (TxObjEditBoxControl) target;
        break;
      case 3:
        this.AddButton = (System.Windows.Controls.Button) target;
        this.AddButton.Click += new RoutedEventHandler(this.AddButton_Click);
        break;
      case 4:
        this.CloseButton = (System.Windows.Controls.Button) target;
        this.CloseButton.Click += new RoutedEventHandler(this.CloseButton_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
